package com.cike.productmanager;

import java.util.ArrayList;

import com.cike.productmanager.util.DataApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class StoreInformation extends Activity {

	private TextView storenameview;
	private TextView storetypeview;
	private TextView storephoneview;
	private TextView storelocationview;
	
	private Button relationbutton;
	private Button activitybutton;
	private Button contButton;
	
	private String storename;
	private String storetype;
	private String storephone;
	private String storelocation;
	//门店ID
	private int storeid;
	
	
	
	//门店在DataApplication.StoreList数组里的index
	private int ID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.storeinformation);
		
		Intent intent=getIntent();
		ID= intent.getIntExtra("ID", 0);
		
		
		
		storenameview = (TextView)findViewById(R.id.storename);
		storetypeview = (TextView)findViewById(R.id.storeType);
		storephoneview = (TextView)findViewById(R.id.storephone);
		storelocationview = (TextView)findViewById(R.id.storelocation);
		
		
		//根据传递过来的index获取门店信息
		storename = DataApplication.storeList.get(ID).getStorename();
		storetype = DataApplication.storeList.get(ID).getStoreType();
		storephone = DataApplication.storeList.get(ID).getStorephone();
		storelocation = DataApplication.storeList.get(ID).getDistrict()+DataApplication.storeList.get(ID).getStreet();
		storeid = DataApplication.storeList.get(ID).getStoreID();
		
		storenameview.setText(storename);
		storetypeview.setText(storetype);
		storephoneview.setText(storephone);
		storelocationview.setText(storelocation);
		
		relationbutton = (Button)findViewById(R.id.relationbutton);
		activitybutton = (Button)findViewById(R.id.activitybutton);
		contButton = (Button)findViewById(R.id.contractbutton);
		
		
		
        
		relationbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		activitybutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//用数组存储搜索出来的活动在ActivityList里面的index和店名
				ArrayList<Integer> index = new ArrayList<Integer>();
				ArrayList<String> name = new ArrayList<String>();
				for(int i=0;i<DataApplication.activityList.size();i++){
					if(DataApplication.activityList.get(i).getStoreBelongID()==storeid){
						index.add(i);
						name.add(((com.cike.productmanager.adt.AActivity)DataApplication.activityList.get(i)).getActivityName());
					}
				}
				
				final int size = name.size();
				final String[] act = (String[]) name.toArray(new String[size]);
				final Integer[] indexs = (Integer[]) index.toArray(new Integer[size]);
				
				
				new AlertDialog.Builder(StoreInformation.this).setTitle("选择活动").setItems(act,new DialogInterface.OnClickListener(){
				      public void onClick(DialogInterface dialog, int which){
				       Toast.makeText(StoreInformation.this, "您已经选择了: "+ act[which],Toast.LENGTH_LONG).show();
				       dialog.dismiss();
				       
				       Intent intent=new Intent(getApplicationContext(),ReadActContent.class);
				       
				       intent.putExtra("ID", indexs[which].intValue());
				       
				       startActivity(intent);
				      }
				   }).show();
			}
		});
		
		
		contButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//用数组存储搜索出来的合同在contractList里面的index和店名
				ArrayList<Integer> index = new ArrayList<Integer>();
				ArrayList<String> name = new ArrayList<String>();
				for(int i=0;i<DataApplication.contractList.size();i++){
					if(DataApplication.contractList.get(i).getStoreBelongID()==storeid){
						index.add(i);
						name.add(((com.cike.productmanager.adt.Contract)DataApplication.contractList.get(i)).getContactName());
					}
				}
				
				final int size = name.size();
				final String[] con = (String[]) name.toArray(new String[size]);
				final Integer[] indexs = (Integer[]) index.toArray(new Integer[size]);
				
				
				
				new AlertDialog.Builder(StoreInformation.this).setTitle("选择合同").setItems(con,new DialogInterface.OnClickListener(){
				      public void onClick(DialogInterface dialog, int which){
				       Toast.makeText(StoreInformation.this, "您已经选择了: "+ con[which],Toast.LENGTH_LONG).show();
				       dialog.dismiss();
				       Intent intent=new Intent(getApplicationContext(),ReadConContent.class);
				       intent.putExtra("ID", indexs[which].intValue());
				       startActivity(intent);
				      }
				   }).show();
			}
		});


		
		
		
	}
	
}
